﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;

namespace SBLib.Common.LogKS
{
    public class LogFuncKS
    {
        public static void WriteToErrorLog(string strMessage, string strErrorFolderLocation)
        {
            try
            {
                string strLogDir = string.Empty;
                string strLogFile = "KS_ErrorLog.txt";


                //=======================================================================================================
                if (string.IsNullOrEmpty(strErrorFolderLocation))
                {
                    strLogDir = GetFilePathFromRelativePhysical("~/ErrorLog");

                }
                //=======================================================================================================


                //create folder 
                //=======================================================================================================
                if (!Directory.Exists(strLogDir))
                {
                    Directory.CreateDirectory(strLogDir);
                }

                strLogFile = Path.Combine(strLogDir, string.Format("{0}-{1}-{2}-{3}", DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, strLogFile));
                //=======================================================================================================


                //Get Calling Method and prepare message
                //====================================================================================================================
                StackTrace stackTrace = new StackTrace();
                string _message = string.Format("{0} (ClassName: {1}, MethodName: {2}) :- {3}", DateTime.Now, stackTrace.GetFrame(1).GetMethod().DeclaringType.Name, stackTrace.GetFrame(1).GetMethod().Name, strMessage);
                //====================================================================================================================


                System.IO.StreamWriter objStreamWriter = null;
                objStreamWriter = System.IO.File.AppendText(strLogFile);

                //Append the the end of the string
                objStreamWriter.WriteLine(_message);

                //Close the stream
                objStreamWriter.Close();
            }
            catch (Exception ex)
            {
                strMessage = "\r\n \r\n Error writing to log file - Message: " + ex.Message + ", InnerException: " + ex.InnerException + ", Stacktrace: " + ex.StackTrace
                         + "\r\n\r\n Original Error message trying to log:  \r\n" + strMessage;


            }

        }

        private static string GetFilePathFromRelativePhysical(string prmPath)
        {
            try
            {
                if (string.IsNullOrEmpty(prmPath) || prmPath.Trim().Length == 0)
                {
                    return "";
                }
                if (prmPath.StartsWith("~"))
                {
                    return System.Web.HttpContext.Current.Server.MapPath(prmPath);
                }
                else
                {
                    return String.Format(prmPath);
                }
            }
            catch (Exception)
            {

                return "";
            }
        }

    }
}
